import matplotlib.pyplot as plt
import numpy as np
All vectors in the vector space has a length except \(0\)-vector, and the terminology of length is norm, the general formula of norm of a vector in \(\mathbb{R}^k\) is given by
\[ \|u\|= \sqrt{x_1^2+x_2^2+...+x_k^2} \]
As an example, let’s plot vectors \((4, 7)\) and \((8, 6)\) in \(\mathbb{R}^2\).
# Define the figure and axis
= plt.subplots(figsize=(8, 8))
fig, ax
# Define the vectors as starting points and directions
= np.array([[0, 0, 4, 7], [0, 0, 8, 6]])
vectors
# Unpack the vectors into separate components
= zip(*vectors)
X, Y, U, V
# Plot the vectors using quiver
="xy", scale_units="xy", scale=1, color="red", alpha=0.6)
ax.quiver(X, Y, U, V, angles
# Set the limits and labels for the plot
0, 10])
ax.set_xlim([0, 10])
ax.set_ylim(["x-axis", fontsize=16)
ax.set_xlabel("y-axis", fontsize=16)
ax.set_ylabel(
# Add grid
ax.grid()
# Annotate the end points of the vectors
4, 7, "$(4, 7)$", fontsize=16)
ax.text(8, 6, "$(8, 6)$", fontsize=16)
ax.text(
# Draw lines to the origin for the first vector
4, 4], [0, 7], color="blue", linewidth=2)
ax.plot([0, 4], [0, 0], color="blue", linewidth=2)
ax.plot([
# Draw lines to the origin for the second vector
8, 0], [0, 0], color="blue", linewidth=2)
ax.plot([8, 8], [0, 6], color="blue", linewidth=2)
ax.plot([
# Annotate the magnitudes of the vectors using Pythagorean theorem
ax.text(1.7, 3.8, r"$\sqrt{4^2+7^2}$", fontsize=16, rotation=np.arctan(7 / 4) * 180 / np.pi
)
ax.text(5, 4.1, r"$\sqrt{8^2+6^2}$", fontsize=16, rotation=np.arctan(6 / 8) * 180 / np.pi
)
# Show the plot
plt.show()
There is also a NumPy function for computing norms: np.linalg.norm()
.
= np.array([4, 7])
a = np.array([8, 6])
b = np.linalg.norm(a)
a_norm = np.linalg.norm(b)
b_norm print("Norm of a and b are {0:.4f}, {1} respectively.".format(a_norm, b_norm))
Norm of a and b are 8.0623, 10.0 respectively.
Let’s try plotting vectors \((1, 1, 1)\), \((-2, 2, 5)\), \((3, -2, 1)\) in \(\mathbb{R}^3\).
= plt.figure(figsize=(10, 10))
fig = fig.add_subplot(111, projection="3d")
ax
= np.array([[0, 0, 0, 1, 1, 1], [0, 0, 0, -2, 2, 5], [0, 0, 0, 3, -2, 1]])
vec = zip(*vec)
X, Y, Z, U, V, W
ax.quiver(
X,
Y,
Z,
U,
V,
W,=1,
length=False,
normalize="red",
color=0.6,
alpha=0.18,
arrow_length_ratio="tail",
pivot="solid",
linestyles=3,
linewidths
)
-5, 5])
ax.set_xlim([-5, 5])
ax.set_ylim([0, 5])
ax.set_zlim([
1, 1, 1, "$(1, 1, 1)$")
ax.text(-2, 2, 5, "$(-2, 2, 5)$")
ax.text(3, -2, 1, "$(3, -2, 1)$")
ax.text(0, 0, 0, "$(0, 0, 0)$")
ax.text(
= np.array(
points
[1, 1, 1], [1, 1, 0]],
[[0, 0, 0], [1, 1, 0]],
[[3, -2, 1], [3, -2, 0]],
[[0, 0, 0], [3, -2, 0]],
[[-2, 2, 5], [-2, 2, 0]],
[[0, 0, 0], [-2, 2, 0]],
[[
]
)
for i in range(points.shape[0]): # loop through the 3rd axis
= zip(*points[i, :, :])
X, Y, Z =2, color="b", alpha=0.7)
ax.plot(X, Y, Z, lw ax.grid()
So the norm of vectors are quite obvious once you see their coordinates.
Vector Addition, Subtraction And Scalar Multiplication
The vector addition is element-wise operation, if we have vectors \(\mathbf{u}\) and \(\mathbf{v}\) addition:
\[\mathbf{u}+\mathbf{v}=\left[\begin{array}{c} u_{1} \\ u_{2} \\ \vdots \\ u_{n} \end{array}\right]+\left[\begin{array}{c} v_{1} \\ v_{2} \\ \vdots \\ v_{n} \end{array}\right]=\left[\begin{array}{c} u_{1}+v_{1} \\ u_{2}+v_{2} \\ \vdots \\ u_{n}+v_{n} \end{array}\right]\]
And subtraction: \[\mathbf{u}+(-\mathbf{v})=\left[\begin{array}{c} u_{1} \\ u_{2} \\ \vdots \\ u_{n} \end{array}\right]+\left[\begin{array}{c} -v_{1} \\ -v_{2} \\ \vdots \\ -v_{n} \end{array}\right]=\left[\begin{array}{c} u_{1}-v_{1} \\ u_{2}-v_{2} \\ \vdots \\ u_{n}-v_{n} \end{array}\right]\]
And the scalar operations: \[c \mathbf{u}=c\left[\begin{array}{c} u_{1} \\ u_{2} \\ \vdots \\ u_{n} \end{array}\right]=\left[\begin{array}{c} c u_{1} \\ c u_{2} \\ \vdots \\ c u_{n} \end{array}\right]\]
Let’s illustrate vector addition \(\mathbf{u}+\mathbf{v}\) in both \(\mathbb{R}^2\) and \(\mathbb{R}^3\).
= plt.subplots(figsize=(9, 9))
fig, ax
= np.array(
vec 0, 0, 4, 7]], [[0, 0, 8, 4]], [[0, 0, 12, 11]], [[4, 7, 8, 4]], [[8, 4, 4, 7]]]
[[[
)= ["r", "b", "g", "b", "r"]
color
for i in range(vec.shape[0]):
= zip(*vec[i, :, :])
X, Y, U, V
ax.quiver(="xy", scale_units="xy", color=color[i], scale=1, alpha=0.6
X, Y, U, V, angles
)
0, 15])
ax.set_xlim([0, 15])
ax.set_ylim(["x-axis", fontsize=16)
ax.set_xlabel("y-axis", fontsize=16)
ax.set_ylabel(
ax.grid()
for i in range(3):
ax.text(=vec[i, 0, 2],
x=vec[i, 0, 3],
y="(%.0d, %.0d)" % (vec[i, 0, 2], vec[i, 0, 3]),
s=16,
fontsize
)
=vec[0, 0, 2] / 2, y=vec[0, 0, 3] / 2, s="$u$", fontsize=16)
ax.text(x=8, y=9, s="$v$", fontsize=16)
ax.text(x=6, y=5.5, s="$u+v$", fontsize=16)
ax.text(x
"Vector Addition", size=18)
ax.set_title( plt.show()
The illustration of \(\mathbf{u} - \mathbf{v}\) is exactly equivalent to \(\mathbf{u} + (-\mathbf{v})\).
= plt.subplots(figsize=(9, 9))
fig, ax
= np.array(
vec
[0, 0, 4, 7]],
[[0, 0, 8, 4]],
[[0, 0, -8, -4]],
[[0, 0, -4, 3]],
[[-8, -4, 4, 7]],
[[4, 7, -8, -4]],
[[8, 4, -4, 3]],
[[
]
)= ["r", "b", "b", "g", "r", "b", "g"]
color
for i in range(vec.shape[0]):
= zip(*vec[i, :, :])
X, Y, U, V
ax.quiver(="xy", scale_units="xy", color=color[i], scale=1, alpha=0.6
X, Y, U, V, angles
)
for i in range(4):
ax.text(=vec[i, 0, 2],
x=vec[i, 0, 3],
y="(%.0d, %.0d)" % (vec[i, 0, 2], vec[i, 0, 3]),
s=16,
fontsize
)
= ["$u$", "$v$", "$-v$", "$u-v$"]
s for i in range(4):
=vec[i, 0, 2] / 2, y=vec[i, 0, 3] / 2, s=s[i], fontsize=16)
ax.text(x=5.8, y=5.8, s="$u-v$", fontsize=16)
ax.text(x
-9, 9])
ax.set_xlim([-9, 9])
ax.set_ylim(["x-axis", fontsize=16)
ax.set_xlabel("y-axis", fontsize=16)
ax.set_ylabel(
ax.grid()"Vector Subtraction", size=18)
ax.set_title( plt.show()
The real number \(c\) in front of vectors are scalars, which are scaling the vectors as its name implies.
= plt.subplots(figsize=(9, 9))
fig, ax
= np.array([[[0, 0, 2, 3]], [[0, 0, 6, 9]], [[0, 0, 2, 2]], [[0, 0, 8, 8]]])
vec = ["r", "b", "r", "b"]
colors
for i in range(vec.shape[0]):
= zip(*vec[i, :, :])
X, Y, U, V
ax.quiver(
X,
Y,
U,
V,="xy",
angles="xy",
scale_units=color[i],
color=1,
scale=0.6,
alpha=-i,
zorder
)
= ["$u$", "$3u$", "$v$", "$3v$"]
s for i in range(vec.shape[0]):
ax.text(=vec[i, 0, 2],
x=vec[i, 0, 3],
y="(%.0d, %.0d)" % (vec[i, 0, 2], vec[i, 0, 3]),
s=16,
fontsize
)=vec[i, 0, 2] / 2, y=vec[i, 0, 3] / 2, s=s[i], fontsize=20)
ax.text(x
0, 10])
ax.set_xlim([0, 10])
ax.set_ylim(["x-axis", fontsize=16)
ax.set_xlabel("y-axis", fontsize=16)
ax.set_ylabel(
ax.grid()
"Vector Multiplication", size=18)
ax.set_title( plt.show()
Now let’s challenge ourselves for plotting in 3D.
= plt.figure(figsize=(10, 10))
fig = fig.add_subplot(111, projection="3d")
ax
############################## Arrows and Texts #####################################
= np.array(
vec
[0, 0, 0, 3, 4, 5]],
[[0, 0, 0, 3, 6, 2]],
[[0, 0, 0, 6, 10, 7]],
[[3, 4, 5, 3, 6, 2]],
[[3, 6, 2, 3, 4, 5]],
[[0, 0, 0, 8, 2, 2]],
[[0, 0, 0, 4, 1, 1]],
[[
]
)= ["r", "b", "g", "b", "r", "r", "b"]
colors
for i in range(vec.shape[0]):
= zip(*vec[i, :, :])
X, Y, Z, U, V, W
ax.quiver(
X,
Y,
Z,
U,
V,
W,=1,
length=False,
normalize=colors[i],
color=0.6,
alpha=0.08,
arrow_length_ratio="tail",
pivot="solid",
linestyles=3,
linewidths
)
ax.text(=vec[i, 0, 3],
x=vec[i, 0, 4],
y=vec[i, 0, 5],
z="$(%.0d, %.0d, %.0d)$" % (vec[i, 0, 3], vec[i, 0, 4], vec[i, 0, 5]),
s
)
############################## Axis #####################################
ax.grid()0, 10])
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
ax.set_zlim(["Vector Addition and Scalar Multiplication", size=16)
ax.set_title( plt.show()
Similarly with \(\mathbf{u}-\mathbf{v}\).
= plt.figure(figsize=(10, 10))
fig = fig.add_subplot(111, projection="3d")
ax
############################## Arrows and Texts #####################################
= np.array(
vec
[0, 0, 0, 3, 4, 5]],
[[0, 0, 0, 3, 6, 2]],
[[0, 0, 0, -3, -6, -2]],
[[0, 0, 0, 0, -2, 3]],
[[-3, -6, -2, 3, 4, 5]],
[[3, 4, 5, -3, -6, -2]],
[[
]
)for i in range(vec.shape[0]):
= zip(*vec[i, :, :])
X, Y, Z, U, V, W
ax.quiver(
X,
Y,
Z,
U,
V,
W,=1,
length=False,
normalize=colors[i],
color=0.08,
arrow_length_ratio="tail",
pivot="solid",
linestyles=3,
linewidths=1 - 0.15 * i,
alpha
)
ax.text(=vec[i, 0, 3],
x=vec[i, 0, 4],
y=vec[i, 0, 5],
z="$(%.0d, %.0d, %.0d)$" % (vec[i, 0, 3], vec[i, 0, 4], vec[i, 0, 5]),
s
)
= ["$u$", "$v$", "$-v$", "$u-v$"]
s for i in range(4):
ax.text(=vec[i, 0, 3] / 2,
x=vec[i, 0, 4] / 2,
y=vec[i, 0, 5] / 2,
z=s[i],
s=15,
size=i + vec.shape[0],
zorder
)
############################## Axis #####################################
ax.grid()-6, 6])
ax.set_xlim([-6, 6])
ax.set_ylim([-3, 6])
ax.set_zlim([
"Vector Subtraction", size=18)
ax.set_title( plt.show()